#include <iostream>
#include <unistd.h>
#include <fcntl.h>
//设置文件描述符为非阻塞
void SetNonBlock(int fd){
    //获取状态标志位
    int fl=fcntl(fd,F_GETFL);
    if(fl<0){
        std::cout<<"文件描述符获取失败!"<<std::endl;
        exit(1);
    }
    fcntl(fd,F_SETFL,O_NONBLOCK|fl);
}
int main(){
    //设置文件为非阻塞
    SetNonBlock(0);

    char buffer [1024];
    while(true){
        int n=read(0,&buffer,sizeof(buffer)-1);
        if(n>0){
            buffer[n]=0;
            std::cout<<"echo#"<<buffer;
        }
        else if(n==0){
            std::cout<<"read over"<<std::endl;
        }
        else{
            if(errno==EAGAIN){
                std::cout<<"EAGAIN中...."<<std::endl;
            }
            else if(errno==EINTR) //硬件中断
            {
                std::cout<<"EINTR中...."<<std::endl;
            }
            else{
                std::cout<<"读取出错!"<<std::endl;
            }
            sleep(1);
        }
    }

    return 0;
}
